﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using SMALLERP.PO;
using ThoughtWorks.QRCode.Codec;
using SMALLERP;
using SMALLERP.QM;
using System.IO;

namespace XJLSC.PM
{
    public partial class CHAXUN : Form
    {
        private readonly DataBase db = new DataBase();
        public delegate void Cdgzzx(object sender, EventArgs e);
        //定义委托对象
        public Cdgzzx cdgzzx;
        public CHAXUN()
        {
            InitializeComponent();
        }

        public string clph
        {
            get
            {
                return _clph;
            }
        }
        string _clph;


        /// <summary>
        /// 用户名
        /// </summary>
        public string clpm
        {
            get
            {
                return _clpm;
            }
        }
        string _clpm;

        /// <summary>
        /// 用户名
        /// </summary>
        public string clgg
        {
            get
            {
                return _clgg;
            }
        }
        string _clgg;

        /// <summary>
        /// 用户名
        /// </summary>
        public string cllbm
        {
            get
            {
                return _cllbm;
            }
        }
        string _cllbm;

        public event EventHandler DataSelected;
        private void CHAXUN_Load(object sender, EventArgs e)
        {
            try
            {
                string strSql = null;

                if (MXCX.LX == "查看物料状况")
                {
                    strSql = "SELECT TB003 品号,RTRIM(MB002)+'  '+RTRIM(MB003) 品名规格,RTRIM(INVMB.UDF01) 适用产品,CEILING(CASE WHEN " + MXCX.SL + "=0 THEN CEILING(TB004) else (CASE WHEN TA015=0 THEN 0 ELSE TB004 / TA015 END) * CEILING(" + MXCX.SL + ") END) 需求数量,CEILING(TB005) 工单已领数量,CEILING(TB004-TB005) 工单未领数量,CEILING(ISNULL(A.MC007,0)) 库存,CMSMC.MC002 仓库,RTRIM(MW002) 工艺名称 FROM MOCTB LEFT JOIN MOCTA ON TB001=TA001 AND TB002=TA002 LEFT JOIN INVMB ON TB003=MB001 LEFT JOIN CMSMC ON TB009=CMSMC.MC001 LEFT JOIN CMSMW ON TB006=MW001 LEFT JOIN V_CKKC A ON A.MC001=MB001 WHERE TB001='" + MXCX.DB + "' AND TB002='" + MXCX.DH + "' AND (CASE WHEN TB006='B15' THEN 'B01' WHEN TB006='P02' THEN 'B01' ELSE TB006 END) ='" + MXCX.GY + "'";
                }
                else if (MXCX.LX == "查看排产状况")
                {
                    strSql = "SELECT  TY001 单别,TY002 单号,TY003 日期,CEILING(TY004) 数量 FROM MOCTY WHERE TY001='" + MXCX.DB + "' AND TY002='" + MXCX.DH + "' AND TY019='" + MXCX.GY + "'";
                }
                else if (MXCXA.LX == "查看排产记录")
                {
                    strSql = "SELECT RQ 排产日期,LX 作业类型,XGSJ 数量,BZ 作业时间,ZYZ 作业者 FROM DATELOG WHERE DB='" + MXCXA.DB + "' AND DH='" + MXCXA.DH + "' AND RQ='" + MXCXA.RQ + "' AND GY='" + MXCXA.GY + "'";
                }
                else if (MXCX.LX == "查看计划完成状况")
                {
                    strSql = "SELECT TY003 排产日期,CEILING(TY004) 计划数量,CEILING(TY006) 完成数量 FROM MOCTY WHERE TY001='" + MXCX.DB + "' AND TY002='" + MXCX.DH + "'";
                }
                else if (MXCX.LX == "查看采购订单")
                {
                    strSql = "SELECT RTRIM(TD021) 订单号,RTRIM(MA002) 供应商,TD004 品号,TD005 品名,TD006 规格,CEILING(TD008) 订单数量,CEILING(TD015) 已交数量,CEILING(TD008-TD015) 未交数量,TD012 预计交货日,TD007 交货仓库,RTRIM(MC002) 仓库名称,TD014 备注,TD001 采购单别,TD002 采购单号,TD003 采购序号,TC003 采购日期 FROM PURTD LEFT JOIN PURTC ON TD001=TC001 AND TD002=TC002 LEFT JOIN CMSMC ON TD007=MC001 LEFT JOIN PURMA ON TC004=MA001 WHERE TD016='N' AND TD004='" + MXCX.DB + "' AND TD018='Y' ORDER BY TC004,TD012";
                    this.Width = 1050;
                    this.Height = 514;
                }
                else if (MXCX.LX == "查看库存")
                {
                    strSql = "SELECT C.ITEM_CODE 品号, B.WAREHOUSE_NAME 仓库,ISNULL(ITEM_LOT.LOT_CODE,'') 批号,CAST(A.INVENTORY_QTY AS REAL) AS 库存 FROM ITEM_WAREHOUSE_BIN AS A LEFT JOIN WAREHOUSE AS B ON A.WAREHOUSE_ID = B.WAREHOUSE_ID LEFT JOIN ITEM C ON A.ITEM_ID=C.ITEM_BUSINESS_ID LEFT JOIN ITEM_LOT ON ITEM_LOT.ITEM_ID=A.ITEM_ID AND ITEM_LOT.ITEM_LOT_ID=A.ITEM_LOT_ID  WHERE (B.INCLUDED_AVAILABLE_QTY = '1') AND C.ITEM_CODE='" + MXCX.DB + "'";
                    this.Width = 320;
                    this.Height = 200;
                }
                else if (MXCX.LX == "查看工单")
                {
                    strSql = "SELECT DOC_NO 工单号,ITEM_CODE 品号,MO.ITEM_DESCRIPTION 品名,MO.ITEM_SPECIFICATION 规格,CAST(PLAN_QTY AS REAL) 预计产量,CAST(REQ_QTY AS REAL) 已生产量,PLAN_COMPLETE_DATE 预计完工日 FROM MO LEFT JOIN ITEM ON MO.ITEM_ID=ITEM.ITEM_BUSINESS_ID WHERE MO.ApproveStatus='Y' AND ITEM_CODE='" + MXCX.DB + "' AND MO.STATUS<>'Y' AND MO.STATUS<>'y' ORDER BY PLAN_COMPLETE_DATE";
                    this.Width = 1300;
                    this.Height = 514;
                }
                else if (MXCX.LX == "查看需求")
                {
                    strSql = "SELECT RTRIM(MW002) 工艺名称,CEILING(XQL) 需求量,DB 单别,DH 单号,RTRIM(MW002) 工艺,ISNULL(TA006,ID) 产品品号,ISNULL(TA034,MB002) 产品品名,ISNULL(TA035,MB003) 产品规格 FROM WLXQ LEFT JOIN MOCTA ON TA001=DB AND TA002=DH LEFT JOIN CMSMW ON WLXQ.GY=MW001 WHERE LB='需求' AND ID='" + MXCX.DB + "' AND XQRQ='" + MXCX.DH + "'";
                    this.Width = 800;
                    this.Height = 300;
                }
                else if (MXCX.LX == "查看销售预测")
                {
                    strSql = "SELECT MF001 预测编号,CEILING(MF008) 预测数量,CEILING(MF009) 受订量,CEILING(MF008-MF009) 预测未交量 FROM COPMF LEFT JOIN COPME ON MF001=ME001 WHERE MF003='" + MXCX.DB + "' AND ME008='Y'";
                    this.Width = 800;
                    this.Height = 300;
                }
                else if (MXCX.LX == "查看订单")
                {
                    strSql = "SELECT RTRIM(B.CUSTOMER_ORDER_NO) 客户单号,C.CUSTOMER_NAME 客户简称,C.CUSTOMER_CODE 客户编号,B.DOC_NO 订单单号,A.SequenceNumber 订单序号,CAST(D.BUSINESS_QTY AS real) 订单数量,CAST(D.DELIVERED_BUSINESS_QTY AS real) 已交数量,CAST(D.BUSINESS_QTY-D.DELIVERED_BUSINESS_QTY AS real) 未交数量 FROM SALES_ORDER_DOC_SD D LEFT JOIN SALES_ORDER_DOC_D A ON D.SALES_ORDER_DOC_D_ID=A.SALES_ORDER_DOC_D_ID LEFT JOIN SALES_ORDER_DOC B ON  A.SALES_ORDER_DOC_ID= B.SALES_ORDER_DOC_ID LEFT JOIN CUSTOMER C ON B.CUSTOMER_ID=C.CUSTOMER_BUSINESS_ID LEFT JOIN ITEM E ON A.ITEM_ID=E.ITEM_BUSINESS_ID WHERE E.ITEM_CODE='" + MXCX.DB + "' AND D.[CLOSE]='0' AND A.ApproveStatus='Y'";
                    this.Width = 800;
                    this.Height = 300;
                }
                else if (MXCX.LX == "查看工单单身")
                {
                    strSql = "SELECT RTRIM(B.PLAN_LOT) 订单号,B.DOC_NO 工单单号,C.ITEM_CODE 产品品号,B.ITEM_DESCRIPTION 产品品名,B.ITEM_SPECIFICATION 产品规格,CAST(B.PLAN_QTY AS REAL) 预计产量,CAST(A.REQU_INCLUDE_MULT_QTY AS real) 需领数量,CAST(A.ISSUED_QTY AS real) 已领数量,(CASE WHEN A.REQU_INCLUDE_MULT_QTY-A.ISSUED_QTY<0 THEN 0 ELSE CAST(A.REQU_INCLUDE_MULT_QTY-A.ISSUED_QTY AS real) END) 未领数量 FROM MO_D A LEFT JOIN MO B ON A.MO_ID=B.MO_ID LEFT JOIN ITEM C ON B.ITEM_ID=C.ITEM_BUSINESS_ID LEFT JOIN ITEM D ON A.ITEM_ID=D.ITEM_BUSINESS_ID WHERE D.ITEM_CODE='" + MXCX.DB + "' AND B.[STATUS]<>'Y' AND B.[STATUS]<>'y'";
                    this.Width = 1050;
                    this.Height = 520;
                }
                else if (MXCX.LX == "查看请购单")
                {
                    strSql = "SELECT TB001 请购单别,TB002 请购单号,TB003 请购序号,CEILING(TB009) 请购数量,CEILING(TB014) 已购数量,CEILING(TB009-TB014) 未购数量 FROM PURTB LEFT JOIN PURTA ON TB001=TA001 AND TB002=TA002 WHERE TB004='" + MXCX.DB + "' AND TB025='Y' AND TB039='N' AND CEILING(TB009-TB014)<>0";
                    this.Width = 1300;
                    this.Height = 520;
                }
                else if (MXCX.LX == "材料品号开窗")
                {
                    strSql = "SELECT TB003 品号,MB002 品名,MB003 规格,TB004 需领数量,ISNULL(INVMB.UDF01,'') 老编码 FROM MOCTB LEFT JOIN INVMB ON TB003=MB001 WHERE TB001='" + MXCX.DB + "' AND TB002='" + MXCX.DH + "'";
                    this.Width = 800;
                    this.Height = 400;
                }
                else if (MXCX.LX == "查看需求数量")
                {
                    strSql = "SELECT TD001 订单单别,TD002 订单单号,TD003 订单序号,ID 品号,A.MB002 品名,A.MB003 规格,ISNULL(INVMB.UDF01,'') 老编码,XQSL 需求数量 FROM DDMXQ A LEFT JOIN INVMB ON ID=MB001 WHERE ID='" + MXCX.DB + "'";
                    this.Width = 800;
                    this.Height = 400;
                }
                else if (MXCX.LX == "查看请购数量")
                {
                    strSql = "SELECT TB001 请购单别,TB002 请购单号,TB003 请购序号,TB004 品号,MB002 品名,MB003 规格,ISNULL(INVMB.UDF01,'') 老编码,TB009 数量 FROM PURTB LEFT JOIN PURTA ON TB001=TA001 AND TB002=TA002 LEFT JOIN INVMB ON TB004=MB001 WHERE TB004='" + MXCX.DB + "' AND TA003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + MXCX.DH + "'),112) AND TA003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + MXCX.GY + "'),112)";

                    this.Width = 800;
                    this.Height = 400;
                }
                else if (MXCX.LX == "查看下单数量")
                {
                    strSql = "SELECT TA001 工单单别,TA002 工单单号,TA006 品号,MB002 品名,MB003 规格,ISNULL(INVMB.UDF01,'') 老编码,TA015 数量 FROM MOCTA LEFT JOIN INVMB ON TA006=MB001 WHERE TA006='" + MXCX.DB + "' AND TA003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + MXCX.DH + "'),112) AND TA003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + MXCX.GY + "'),112)";
                    this.Width = 800;
                    this.Height = 400;
                }
                else if (MXCX.LX == "查看订单采购状况")
                {
                    strSql = "SELECT TC003 采购日期,TD001 单别,TD002 单号,TD003 序号,TD004 品号,TD005 品名,TD006 规格,TD009 单位,CEILING(TD008) 采购数量,CEILING(TD015) 已交数量,CEILING(TD008-TD015) 未交数量,TD012 预计交货日,TD014 备注 FROM PURTD LEFT JOIN PURTC ON TD001=TC001 AND TD002=TC002 WHERE TD021='" + MXCX.DH + "'";

                    this.Width = 800;
                    this.Height = 400;
                }
                else if (MXCX.LX == "查看需领用量")
                {
                    strSql = "SELECT RTRIM(MW002) 工艺名称,CEILING(XQL) 需求量,DB 单别,DH 单号,RTRIM(MW002) 工艺,ISNULL(TA006,ID) 产品品号,ISNULL(TA034,INVMB.MB002) 产品品名,ISNULL(TA035,INVMB.MB003) 产品规格 FROM WLXQ LEFT JOIN MOCTA ON TA001=DB AND TA002=DH LEFT JOIN CMSMW ON WLXQ.GY=MW001 LEFT JOIN INVMB ON INVMB.MB001=ID WHERE LB='需求' AND ID='" + MXCX.DB + "' AND CONVERT(CHAR(8),DATEADD(DAY,INVMB.MB036*(-1),XQRQ),112)<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + MXCX.GY + "'),112)";
                    this.Width = 800;
                    this.Height = 300;
                }

                if (HXMXCX.LX == "正常领料")
                {
                    strSql = "SELECT SCDW 生产单位,JYRQ 交易日期,DDDH 订单号,DB 单别,DH 单号,XH 序号,GDDB 工单单别,GDDH 工单单号,CEILING(ZCLL) 数量 FROM V_BTHX WHERE (SCDW='" + HXMXCX.SCDW + "' OR '" + HXMXCX.SCDW + "'='') AND JYRQ>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.KSRQ + "'),112) AND (JYRQ<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.JSRQ + "'),112) OR '" + HXMXCX.JSRQ + "'='') AND DDDH LIKE '%" + HXMXCX.DDH + "%' AND PH='" + HXMXCX.PH + "' AND ZCLL<>0";
                    //MessageBox.Show(strSql, "1");
                    this.Width = 1200;
                    this.Height = 400;
                }
                else if (HXMXCX.LX == "返工领料")
                {
                    strSql = "SELECT SCDW 生产单位,JYRQ 交易日期,DDDH 订单号,DB 单别,DH 单号,XH 序号,GDDB 工单单别,GDDH 工单单号,CEILING(FGLL) 数量 FROM V_BTHX WHERE (SCDW='" + HXMXCX.SCDW + "' OR '" + HXMXCX.SCDW + "'='') AND JYRQ>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.KSRQ + "'),112) AND (JYRQ<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.JSRQ + "'),112) OR '" + HXMXCX.JSRQ + "'='') AND DDDH LIKE '%" + HXMXCX.DDH + "%' AND PH='" + HXMXCX.PH + "' AND FGLL<>0";
                    //MessageBox.Show(strSql, "1");
                    this.Width = 1200;
                    this.Height = 400;
                }
                else if (HXMXCX.LX == "正常入库")
                {
                    strSql = "SELECT SCDW 生产单位,JYRQ 交易日期,DDDH 订单号,DB 单别,DH 单号,XH 序号,GDDB 工单单别,GDDH 工单单号,CEILING(ZCRK) 数量 FROM V_BTHX WHERE (SCDW='" + HXMXCX.SCDW + "' OR '" + HXMXCX.SCDW + "'='') AND JYRQ>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.KSRQ + "'),112) AND (JYRQ<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.JSRQ + "'),112) OR '" + HXMXCX.JSRQ + "'='') AND DDDH LIKE '%" + HXMXCX.DDH + "%' AND PH='" + HXMXCX.PH + "' AND ZCRK<>0";
                    //MessageBox.Show(strSql, "1");
                    this.Width = 1200;
                    this.Height = 400;
                }
                else if (HXMXCX.LX == "返工入库")
                {
                    strSql = "SELECT SCDW 生产单位,JYRQ 交易日期,DDDH 订单号,DB 单别,DH 单号,XH 序号,GDDB 工单单别,GDDH 工单单号,CEILING(FGRK) 数量 FROM V_BTHX WHERE (SCDW='" + HXMXCX.SCDW + "' OR '" + HXMXCX.SCDW + "'='') AND JYRQ>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.KSRQ + "'),112) AND (JYRQ<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.JSRQ + "'),112) OR '" + HXMXCX.JSRQ + "'='') AND DDDH LIKE '%" + HXMXCX.DDH + "%' AND PH='" + HXMXCX.PH + "' AND FGRK<>0";
                    //MessageBox.Show(strSql, "1");
                    this.Width = 1200;
                    this.Height = 400;
                }
                else if (HXMXCX.LX == "工废数量")
                {
                    strSql = "SELECT SCDW 生产单位,JYRQ 交易日期,DDDH 订单号,DB 单别,DH 单号,XH 序号,GDDB 工单单别,GDDH 工单单号,CEILING(GFSL) 数量 FROM V_BTHX WHERE (SCDW='" + HXMXCX.SCDW + "' OR '" + HXMXCX.SCDW + "'='') AND JYRQ>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.KSRQ + "'),112) AND (JYRQ<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.JSRQ + "'),112) OR '" + HXMXCX.JSRQ + "'='') AND DDDH LIKE '%" + HXMXCX.DDH + "%' AND PH='" + HXMXCX.PH + "' AND GFSL<>0";
                    //MessageBox.Show(strSql, "1");
                    this.Width = 1200;
                    this.Height = 400;
                }
                else if (HXMXCX.LX == "料废数量")
                {
                    strSql = "SELECT SCDW 生产单位,JYRQ 交易日期,DDDH 订单号,DB 单别,DH 单号,XH 序号,GDDB 工单单别,GDDH 工单单号,CEILING(LFSL) 数量 FROM V_BTHX WHERE (SCDW='" + HXMXCX.SCDW + "' OR '" + HXMXCX.SCDW + "'='') AND JYRQ>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.KSRQ + "'),112) AND (JYRQ<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.JSRQ + "'),112) OR '" + HXMXCX.JSRQ + "'='') AND DDDH LIKE '%" + HXMXCX.DDH + "%' AND PH='" + HXMXCX.PH + "' AND LFSL<>0";
                    //MessageBox.Show(strSql, "1");
                    this.Width = 1200;
                    this.Height = 400;
                }
                else if (HXMXCX.LX == "不良数量")
                {
                    strSql = "SELECT SCDW 生产单位,JYRQ 交易日期,DDDH 订单号,DB 单别,DH 单号,XH 序号,GDDB 工单单别,GDDH 工单单号,CEILING(BLSL) 数量 FROM V_BTHX WHERE (SCDW='" + HXMXCX.SCDW + "' OR '" + HXMXCX.SCDW + "'='') AND JYRQ>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.KSRQ + "'),112) AND (JYRQ<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.JSRQ + "'),112) OR '" + HXMXCX.JSRQ + "'='') AND DDDH LIKE '%" + HXMXCX.DDH + "%' AND PH='" + HXMXCX.PH + "' AND BLSL<>0";
                  
                    this.Width = 1200;
                    this.Height = 400;
                }
                else if (HXMXCX.LX == "入库数量")
                {
                    strSql = "SELECT TF003 入库日期,ISNULL(TA027,'') 订单号,TG001 单别,TG002 单号,TG003 序号,GDDB 工单单别,GDDH 工单单号,CEILING(TG013) 数量 FROM MOCTG LEFT JOIN MOCTF ON TG001=TF001 AND TG002=TF002 LEFT JOIN MOCTA ON TG014=TA001 AND TG015=TA002 WHERE TF003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.KSRQ + "'),112) AND (TF003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.JSRQ + "'),112) OR '" + HXMXCX.JSRQ + "'='') AND TG004='" + HXMXCX.PH + "' AND TG013<>0";

                    this.Width = 800;
                    this.Height = 400;
                }
                else if (HXMXCX.LX == "订单数量")
                {
                    strSql = "SELECT RTRIM(MA002) 客户简称,RTRIM(MV002) 业务员,TC003 入库日期,TD002 订单号,TD001 单别,TD002 单号,TD003 序号,CEILING(TD008) 数量,CEILING(TD012) 金额 FROM COPTD LEFT JOIN COPTC ON TD001=TC001 AND TD002=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN CMSMV ON TC006=MV001 WHERE TC003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.KSRQ + "'),112) AND (TC003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.JSRQ + "'),112) OR '" + HXMXCX.JSRQ + "'='') AND TD004='" + HXMXCX.PH + "' AND TD008<>0 AND TD004 LIKE '8%'";

                    this.Width = 800;
                    this.Height = 400;
                }
                else if (HXMXCX.LX == "出库数量")
                {
                    strSql = "SELECT TG003 销货日期,TG004 客户编号,RTRIM(MA002) 客户简称,ISNULL(TH015,'') 订单号,TH001 单别,TH002 单号,TH003 序号,CEILING(TH008) 数量 FROM COPTH LEFT JOIN COPTG ON TG001=TH001 AND TG002=TH002 LEFT JOIN COPMA ON TG004=MA001 WHERE TG003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.KSRQ + "'),112) AND (TG003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + HXMXCX.JSRQ + "'),112) OR '" + HXMXCX.JSRQ + "'='') AND TH004='" + HXMXCX.PH + "' AND TH008<>0";

                    this.Width = 800;
                    this.Height = 400;
                }

                if (MXCXB.LX == "查看生产进度")
                {
                    strSql = "SELECT A.TA001 工单单别,A.TA002 工单单号,RTRIM(TA034) 产品名称,ISNULL(MG007,MW002) 工序,CAST(TA015 AS REAL) 预计产量,CAST(ISNULL((SELECT SUM(TE011) FROM SFCTE WHERE TE006=A.TA001 AND TE007=A.TA002 AND TE008=MF003 AND RTRIM(SFCTE.UDF08)=RTRIM(MG006)),0) AS REAL) 已派工数量,CAST(ISNULL((SELECT SUM(TE011+SFCTE.UDF51+SFCTE.UDF52+SFCTE.UDF53) FROM SFCTE WHERE TE006=A.TA001 AND TE007=A.TA002 AND TE008=MF003 AND RTRIM(SFCTE.UDF08)=RTRIM(MG006)),0) AS REAL) 已报工数量 FROM MOCTA A LEFT JOIN INVMB ON A.TA006=MB001 LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN BOMMG ON MF001=MG001 AND MF002=MG002 AND MF003=MG003 LEFT JOIN CMSMW ON MGD01=MW001 WHERE A.TA026='" + MXCXB.DB + "' AND A.TA027='" + MXCXB.DH + "' AND A.TA028='" + MXCXB.XH + "' AND TA021='" + MXCXB.GY + "' ORDER BY A.TA006,MF003,MG006";
                    this.Width = 800;
                    this.Height = 400;
                }

                //textBox1.Text = strSql;
                //return;
                dataGridView1.DataSource = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];

                if (MXCX.LX == "查看物料状况")
                {
                    this.dataGridView1.Columns["品名规格"].Width = 400;
                }
                else if (MXCX.LX == "查看排产状况")
                {

                }
                else if (MXCX.LX == "查看计划完成状况")
                {

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }

        }

        private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (MXCX.LX != "材料品号开窗")
            {
                string x1 = "";
                string x2 = "";
                double x3 = 0.00;
                string x4 = "";
                string x5 = "";
                x1 = dataGridView1["品号", dataGridView1.CurrentCell.RowIndex].Value.ToString();
                x2 = this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString();
                x3 = Convert.ToDouble(this.dataGridView1.CurrentCell.Value.ToString());

                if (e.RowIndex > -1 && e.ColumnIndex == this.dataGridView1.Columns["库存"].Index)
                {
                    x4 = "查看采购订单";
                }

                else
                {
                    x4 = "其他";
                    MessageBox.Show("本列无明细数据", "软件提示");
                }

                if (x4 != "其他")
                {
                    MXCX xx = new MXCX(x1, x2, x3, x4, x5);
                    CHAXUN f = new CHAXUN();
                    f.cdgzzx += new CHAXUN.Cdgzzx(textBox2_TextChanged);
                    f.Show(this);
                }
            }


        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.DataSelected != null)
            {

                this._clph = dataGridView1.Rows[e.RowIndex].Cells["品号"].Value.ToString();
                this._clpm = dataGridView1.Rows[e.RowIndex].Cells["品名"].Value.ToString();
                this._clgg = dataGridView1.Rows[e.RowIndex].Cells["规格"].Value.ToString();
                this._cllbm = dataGridView1.Rows[e.RowIndex].Cells["老编码"].Value.ToString();
                // 引发事件
                this.DataSelected(null, null);
            }
            this.Close();  // 关闭本窗体
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {
                if (MXCX.LX == "查看物料状况")
                {
                    double gdwl = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["工单未领数量"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["工单未领数量"].Index].Value.ToString());
                    double kcsl = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["库存"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["库存"].Index].Value.ToString());
                    if (gdwl >kcsl)
                    {

                        for (int i = 0; i <= this.dataGridView1.Columns.Count - 1; i++)
                        {
                            this.dataGridView1.Rows[e.RowIndex].Cells[i].Style.BackColor = Color.Yellow;
                        }
                    }
                }
            }

            catch
            {
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存为Excel文件";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stream myStream;
                myStream = dlg.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string columnTitle = null;
                try
                {
                    //写入列标题    
                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                    {
                        if (dataGridView1.Columns[i].Visible == true)
                        {
                            if (i > 0)
                            {
                                columnTitle += "\t";
                            }
                            columnTitle += dataGridView1.Columns[i].HeaderText;

                        }

                    }
                    sw.WriteLine(columnTitle);
                    //写入列内容    
                    for (int j = 0; j < dataGridView1.Rows.Count; j++)
                    {

                        string columnValue = null;
                        for (int k = 0; k < dataGridView1.Columns.Count; k++)
                        {
                            if (dataGridView1.Columns[k].Visible == true)
                            {
                                if (k > 0)
                                {
                                    columnValue += "\t";
                                }
                                if (dataGridView1.Rows[j].Cells[k].Value == null)
                                    columnValue += "";
                                else
                                    columnValue += dataGridView1.Rows[j].Cells[k].Value.ToString().Trim();
                            }
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
        }
    }
}
